package com.ducanh.doan.ui;

import com.ducanh.doan.R;
import com.ducanh.doan.SharedPreferencesHelper;
import com.ducanh.doan.ui.screen.BaseFragment;
import com.ducanh.doan.ui.screen.MainFragment;
import com.ducanh.doan.ui.screen.SetupFragment;
import com.ducanh.doan.ui.screen.BaseFragment.FragmentCallBack;
import com.ducanh.doan.utils.DialogUtils;
import com.ducanh.doan.utils.LanguageUtils;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements FragmentCallBack {

	BaseFragment currentFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferencesHelper sharepref = new SharedPreferencesHelper(getApplicationContext());
		String localeCode = sharepref.getLocale();
		LanguageUtils.changeLanguage(this, localeCode);
		setContentView(R.layout.activity_main);
		Bundle args = new Bundle();
		if (sharepref.isFirstLaunch()) {
			SetupFragment f = SetupFragment.newInstance();
			// if want to get back from main screen to setup screen
			// getSupportFragmentManager().beginTransaction().add(f.getContainerId(), f, f.getFragmentTag()).commit();
			getSupportFragmentManager().beginTransaction().add(f.getContainerId(), f, f.getFragmentTag()).commit();
		} else {
			MainFragment mainFragment = MainFragment.newInstance(args);
			getSupportFragmentManager().beginTransaction().add(mainFragment.getContainerId(), mainFragment).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		if (currentFragment != null) {
			currentFragment.onBackPressed();
		}
	}

	@Override
	public void onActivityOnBack() {
		DialogUtils.showExitDialog(this);
	}

	@Override
	public void setCurrentFragment(BaseFragment f) {
		currentFragment = f;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
	}

	
}
